package cn.cocho.custom.yunCashierDf.common.controller;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by WQJ on 2017-05-29.
 */
public class HttpSubmit {

    public String postAttendanceInfo(String jsonString,String url){
        HttpClient httpClient = new HttpClient();
        // 设置连接超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60*1000);
        // 设置读取超时时间(单位毫秒)
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30*1000);
        PostMethod method = new PostMethod(url);
        String info = null;
        try {
            RequestEntity entity = new StringRequestEntity(jsonString, "application/json", "UTF-8");
            method.setRequestEntity(entity);
            httpClient.executeMethod(method);
            int code = method.getStatusCode();
            if (code == HttpStatus.SC_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(),"UTF-8"));
                StringBuffer stringBuffer = new StringBuffer();
                String str = "";
                while ((str = reader.readLine()) != null) {
                    stringBuffer.append(str);
                }
                info = stringBuffer.toString();
                System.out.println("bbchat 返回报文：" + info);
            }else{
                System.out.println("bbchat 接口返回失败  httpStatusCode="+code);
            }
        } catch (Exception ex) {
            System.out.println("内部接口报文发送异常:"+ ex.getMessage());
            ex.printStackTrace();
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
        return info;
    }

}
